package com.xuetang9.t9.hotel.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName TokenInterceptor
 * @Description TODO
 * @Author qin
 * @Date 2020/3/1 14:43
 * @Version 1.0
 **/
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("token");
        System.out.println(token);
        if(token != null) {
            boolean result = TokenLogin.verifyToken(token);
            if (result) {
                System.out.println("通过拦截器");
                return true;
            }
        }
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        try{
//            JSONObject json = new JSONObject();
//            json.put("msg","token verify fail");
//            json.put("code","50000");
//            response.getWriter().append(json.toJSONString());
//            System.out.println("认证失败，未通过拦截器");
//        }catch (Exception e){
//            e.printStackTrace();
            response.sendError(403);
//            return false;
//        }
            return false;
    }
}
